﻿using DTO;
using IService;
using NPOI.HSSF.UserModel;
using Service;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace Web.Controllers
{
    public class HomeController : Controller
    {
        //IOC属性调用[第一种调用方法]
        //public IJobService Serber { get; set; }


        //IOC构造函数调用[第二种调用方法]
        private readonly IJobService Serber;
        public HomeController(IJobService jobService)
        {
            Serber = jobService;
        }

        public ActionResult Index()
        {
            //IJobService jobService = new JobService();
            //var list = jobService.GetList();
            var list = Serber.GetList();
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetExcel() 
        {
            //调用NPOI 
            var list = Serber.GetList();
            Dictionary<string, string> FiedNames = new Dictionary<string, string>();
            FiedNames.Add("职位名称", "Name");
            FiedNames.Add("职位代码", "Code");
            HSSFWorkbook wb = null;
            wb = Commer.ExcelHelper.BuildSwitchData<DTO.JobDTO>("职位", list, FiedNames);//调用通用方法
            string sExportFileName = "职位" + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";//设置下载文件的名称
            MemoryStream stream = new MemoryStream();
            wb.Write(stream);
            var buf = stream.ToArray();
            return File(buf, "pplication/vnd.ms-excel", sExportFileName);
        }
       
        public ActionResult Com()
        {
            return View();
        }

        public ActionResult ShowJobPageList(int offset, int limit,string name,string code, string sort, string order)
        {
            int count = 0;   //总数据量

            if (string.IsNullOrWhiteSpace(sort))
            {
                sort = "Id";
            }
            var lst = Serber.ShowJobPageList(offset, limit,  name,code , ref count, sort, order);



            return Json(new { total = count, rows = lst }, JsonRequestBehavior.AllowGet);
        }

       
        public ActionResult Del(string id)
        {
            JobDTO job = new JobDTO();
            string[] jid = id.Split(',');
            string str = "";
            for (int i = 0; i < jid.Length; i++)
            {
                job.Id = jid[i];
                Serber.Del(job);
                str = "OK";
            }
            if (str == "OK")
            {
                return Content("OK");
            }
            else
            {
                return Content("Error");
            }

        }
        public ActionResult Upda(string name, string code, string id)
        {
            JobDTO job = new JobDTO();
            job.Name = name;
            job.Code = code;
            job.Id = id;
            if (Serber.Update(job))
            {
                return Content("OK");
            }
            else
            {
                return Content("Error");
            }
        }
        public ActionResult GetUpda(string id)
        {

            var mode = Serber.JobQueryId(id);
            return Json(mode, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Add(string name, string code)
        {
            JobDTO job = new JobDTO();
            job.Name = name;
            job.Code = code;
            if (Serber.Add(job))
            {
                return Content("OK");
            }
            else
            {
                return Content("Error");
            }

        }


    }
}